﻿using BasicInformation.ApiRead.Application.Command.Product;
using BasicInformation.Domain;
using BasicInformation.Dtos;
using BasicInformation.ErrorCode;
using BasicInformation.Infrastructure.Implement;
using BasicInformation.Infrastructure.Interface;
using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using System.Text;

namespace BasicInformation.ApiRead.Application.Handler.Product
{
    public class GetProductHandler : IRequestHandler<GetProductCommand, ApiPaging<ProductDTO>>
    {
        IDatabaseContext db;

        public GetProductHandler(IDatabaseContext db)
        {
            this.db = db;
        }

        public async Task<ApiPaging<ProductDTO>> Handle(GetProductCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiPaging<ProductDTO>();

            var countSql = new StringBuilder();
            var sqlBuilder = new StringBuilder();

            sqlBuilder.Append(@"SELECT
	                            a.ProductId,
	                            a.ProductCode,
	                            a.ProductName,
	                            a.ManufacturerCode,
	                            a.InternalCode,
	                            a.Specifications,
	                            b.CategoryId,
	                            b.CategoryName,
	                            c.UnitId,
	                            c.UnitName,
	                            a.EarlyXia,
	                            a.EarlyShang,
	                            a.PackagingType,
	                            a.Price,
	                            a.Weight,
	                            a.SupplierName,
	                            d.DepotId,
	                            a.LocationId,
	                            e.ClientId,
	                            a.CategoryRemark 
                            FROM
	                            ProductModel a
	                            JOIN CategoryModel b ON a.CategoryId = b.CategoryId
	                            JOIN UnitModel c ON a.UnitId = c.UnitId
	                            JOIN DepotModel d ON a.DepotId = d.DepotId
	                            JOIN ClientModel e ON a.ClientId = e.ClientId 
                            WHERE
	                            a.IsDel = 0 
                                AND b.IsDel=0 AND c.IsDel=0 AND d.IsDel=0 AND e.IsDel=0 ");

            countSql.Append(@"SELECT COUNT(a.ProductId) FROM ProductModel a  
                                JOIN CategoryModel b on a.CategoryId=b.CategoryId   
                                JOIN UnitModel c on a.UnitId = c.UnitId                               
                                JOIN DepotModel d on a.DepotId=d.DepotId                              
                                JOIN ClientModel e on a.ClientId =e.ClientId WHERE a.IsDel=0 
                                AND b.IsDel=0 AND c.IsDel=0 AND d.IsDel=0 AND e.IsDel=0 ");

            var parameters = new DynamicParameters();

            if (!string.IsNullOrEmpty(request.ProductCode))
            {
                sqlBuilder.Append("AND a.ProductCode = @ProductCode ");
                parameters.Add("ProductCode", request.ProductCode);
                countSql.Append("AND a.ProductCode = @ProductCode ");
            }
            if (!string.IsNullOrEmpty(request.ProductName))
            {
                sqlBuilder.Append(" AND a.ProductName LIKE @ProductName ");
                parameters.Add("ProductName", $"%{request.ProductName}%");
                countSql.Append(" AND a.ProductName LIKE @ProductName ");
            }

            //if (request.id != null)
            //{
            //    sqlBuilder.Append("AND a.ProductId = @ProductId ");
            //    parameters.Add("ProductId", request.);
            //    countSql.Append("AND a.ProductId = @ProductId ");
            //}

            if (request.CategoryId != null)
            {
                sqlBuilder.Append("AND a.CategoryId = @CategoryId ");
                parameters.Add("CategoryId", request.CategoryId);
                countSql.Append("AND a.CategoryId = @CategoryId ");
            }
            sqlBuilder.Append("ORDER BY a.ProductId DESC ");
            sqlBuilder.Append("LIMIT @size OFFSET @Offset ");
            parameters.Add("size", request.size);
            parameters.Add("Offset", (request.index - 1) * request.size);

           

            var data = await db.DapperConnection.QueryAsync<ProductDTO>(sqlBuilder.ToString(), parameters);
            var total = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parameters);

            result.Msg = "显示成功";
            result.Code = ApiEnum.OK;
            result.Data = data.ToList();
            result.TotalCount = total;
            result.TotalPCount = (int)Math.Ceiling((double)total / request.size);
            return result;
        }
    }
}
